package test;

import Bean.BookInfoEntity;
import Bean.UserinfoEntity;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import tools.HibernateTools;

public class testHibernate {
       //gaohiber--来自高金磊
           static Configuration configuration;
           static SessionFactory sessionFactory;
           static Session session;
           static Transaction transaction;
       private static void  init(){
           configuration=new Configuration().configure();
           sessionFactory=configuration.buildSessionFactory();
           session=sessionFactory.openSession();
           transaction=session.beginTransaction();
       }

       private static void destory(){
           transaction.commit();
           session.close();
           sessionFactory.close();
           System.out.println("检测完毕,数据库连接成功");
       }
       public static void main(String arg[]) {//main--来自高金磊

           init();
           destory();
           Session session= HibernateTools.getSession();
           for (Object from_userinfoEntity_ : (session.createQuery("from UserinfoEntity ").list())) {
               System.out.println(((UserinfoEntity)from_userinfoEntity_).getUsername());
           }
           //懒加载测试
           BookInfoEntity bookInfoEntity = session.load(BookInfoEntity.class,177);
           bookInfoEntity.getBook_name();
           bookInfoEntity.getBook_picture();
           HibernateTools.commit(session);
       }

}
